package com.zili.transformation;

import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.scala.typeutils.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author : ranzlupup
 * @date : 2023/3/7 16:49
 */
public class TransformFlatmapTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> stream = env.readTextFile("input/words.txt");

        SingleOutputStreamOperator<String> flatMapStream = stream.flatMap((String line, Collector<String> out) -> {
                    String[] words = line.split(" ");
                    for (String word : words) {
                        out.collect(word);
                    }
                })
                // .returns(Types.STRING());
                .returns(new TypeHint<String>() {
                });
        flatMapStream.print();
        env.execute();
    }
}
